﻿using System;
using System.Globalization;
using System.Windows.Forms;
using HighDiveEngine.Model;
using HighDiveEngine.Resources;

namespace HighDiveAdmin.GUI.Components
{
    /// <summary>
    /// Control that handles editing a dive in the divequeue
    /// </summary>
    public partial class RunningContestEditDiveInQueue : UserControl
    {
        /// <summary>
        /// Triggered when the Discard button is clicked
        /// </summary>
        public event MethodInvoker EventDiscard;
        /// <summary>
        /// Triggered when the Save button is clicked
        /// </summary>
        public event DelegateVoidStringIntInt EventSave;
        /// <summary>
        /// Triggered when the dive is being repositioned in the queue
        /// </summary>
        public event DelegateDiveListInt EventQueueReposition;
        public RunningContestEditDiveInQueue()
        {
            InitializeComponent();
        }

        #region ButtonClickEvents
        private void buttonSave_Click(object sender, EventArgs e)
        {
            if (EventSave != null)
                EventSave(textBoxCode.Text, Convert.ToInt32(textBoxHeight.Text), trackBarReposition.Value);
        }

        private void buttonDiscard_Click(object sender, EventArgs e)
        {
            if (EventDiscard != null)
                EventDiscard();
        }

        private void trackBarReposition_ValueChanged(object sender, EventArgs e)
        {
            if (EventQueueReposition == null) return;

            var dives = EventQueueReposition(trackBarReposition.Value);
            labelPrevious.Text = dives[0].Diver.FirstName + @" " + dives[0].Diver.LastName;
            labelNext.Text = dives[1].Diver.FirstName + @" " + dives[1].Diver.LastName;
            labelDiveNumber.Text = trackBarReposition.Value.ToString();
        }
        #endregion

        #region FillFunctions
        /// <summary>
        /// Fills the control with all the informaton about the dive that's about to be edited
        /// </summary>
        /// <param name="dive">Dive to edit</param>
        public void FillDiveInfo(Dive dive)//, Dive previous, Dive next, int numDives)
        {
            //labelPrevious.Text = previous.Diver.FirstName + @" " + previous.Diver.LastName;
            //labelNext.Text = next.Diver.FirstName + @" " + next.Diver.LastName;
            //trackBarReposition.Maximum = numDives;
            trackBarReposition.Value = Convert.ToInt32(dive.QueueOrderNumber);
            labelContestant.Text = dive.Diver.FirstName + @" " + dive.Diver.LastName;
            labelDifficulty.Text = Convert.ToDouble(dive.Difficulty).ToString(CultureInfo.InvariantCulture);
            labelDiveNumber.Text = dive.DiveNumber.ToString();
            textBoxCode.Text = dive.DiveCode;
            textBoxHeight.Text = dive.DiveHeight.ToString();
        }
        #endregion

        #region TabStop
        private void RunningContestEditDiveInQueue_TabStopChanged(object sender, EventArgs e)
        {
            foreach (Control control in Controls)
                control.TabStop = TabStop;
        }
        private void textBoxCode_VisibleChanged(object sender, EventArgs e)
        {
            if (Visible)
                textBoxCode.Focus();
        }
        #endregion
    }
}
